import NextAuth from "next-auth"; import CredentialsProvider from "next-auth/providers/credentials"; import * as R from "ramda"; const authOptions = { // https://generate-secret.vercel.app/32 secret: process.env.SECRET, // Configure one or more authentication providers providers: [ CredentialsProvider({ // The name to display on the sign in form (e.g. 'Sign in with...') name: "Cocorobo cloud", // The credentials is used to generate a suitable form on the sign in page. // You can specify whatever fields you are expecting to be submitted. // e.g. domain, username, password, 2FA token, etc. // You can pass any HTML attribute to the tag through the object. credentials: { userid: { label: 'userid', type: 'text' } // loginUsername: { label: "用户名", type: "text" }, // loginPassword: { label: "密码", type: "password" }, }, async authorize(credentials, req) { return { userid: credentials.userid }; // You need to provide your own logic here that takes the credentials // submitted and returns either a object representing a user or value // that is false/null if the credentials are invalid. // e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' } // You can also use the `req` object to obtain additional parameters // (i.e., the request IP address) /* const res = await fetch("https://beta.api.cocorobo.cn/api/user", { method: "POST", body: JSON.stringify( R.pick(["loginUsername", "loginPassword"], credentials) ), headers: { "Content-Type": "application/json", Origin: "https://edu.cocorobo.cn", }, }); if (res.status !== 200) { return null; } const resJson = await res.json(); const user = resJson?.[0]?.[0]; // If no error and we have user data, return it if (res.ok && user && user.active) { return { ...user, id: user.userid, name: user.username }; } */ }, }), ], callbacks: { async session({ session, user }) { console.log(session, user); // Send properties to the client, like an access_token from a provider. session.user.id = user.userid; try { const res = await fetch( `https://pbl.cocorobo.cn/api/pbl/selectUser?userid=${user.userid}`, { method: "GET", headers: { "Content-Type": "application/json", }, } ); const username = (await res.json())?.[0]?.[0]?.username; session.user.name = username; } catch (e) { return null; } return session; }, }, }; const handler = NextAuth(authOptions); export { handler as GET, handler as POST };